<template>
    <BasicLayout>
        <template #wrapper>
            <el-card class="box-card">
                <el-form ref="queryForm" :model="queryParams" :inline="true">

                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb10" style="float:right">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">
                            <span>新增</span>
                        </el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple"
                            @click="handleDelete">删除</el-button>
                    </el-col>
                </el-row>

                <el-table v-loading="loading" :data="dataList" border @sort-change="handleSortChang">
                    <el-table-column label="编码" sortable="custom" width="75" type="index" />
                    <el-table-column label="平台" prop="platform" :show-overflow-tooltip="true" />
                    <el-table-column label="版本号" prop="version_android" :show-overflow-tooltip="true" />
                    <el-table-column label="下载地址" prop="price" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <span>{{ scope.row.android ? scope.row.android : scope.row.ios }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="更新内容" prop="msg" :show-overflow-tooltip="true" />
                    <el-table-column label="操作" class-name="small-padding fixed-width" width="120">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit" :disabled="!multiple && single"
                                @click="handleUpdate(scope.row)">修改</el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageIndex"
                    :limit.sync="queryParams.pageSize" @pagination="getList" />

                <!-- 添加或修改对话框 -->
                <el-drawer ref="drawer" :title="title" :visible.sync="open" direction="rtl" custom-class="demo-drawer">
                    <div class="demo-drawer__content">
                        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                            <el-form-item label="发布平台" prop="platform" @change="selectSystem">
                                <el-select v-model="form.platform" filterable placeholder="请选择发布平台">
                                    <el-option key="huawei" label="华为" value="huawei" />
                                    <el-option key="xiaomi" label="小米" value="xiaomi" />
                                    <el-option key="oppo" label="oppo" value="oppo" />
                                    <el-option key="vivo" label="vivo" value="vivo" />
                                    <el-option key="apple" label="苹果" value="apple" />
                                </el-select>
                            </el-form-item>
                            <el-form-item label="版本状态" prop="status">
                                <el-switch v-model="form.status" active-value="success" inactive-value="fail"
                                    active-text="启用" inactive-text="禁用" />
                            </el-form-item>
                            <el-form-item label="发布版本"
                                :prop="form.platform !== 'apple' ? 'version_android' : 'version_ios'">
                                <el-input v-model="form[form.platform !== 'apple' ? 'version_android' : 'version_ios']"
                                    placeholder="请输入版本" />
                            </el-form-item>
                            <el-form-item label="发布地址" :prop="form.platform !== 'apple' ? 'android' : 'ios'">
                                <el-input v-model="form[form.platform !== 'apple' ? 'android' : 'ios']"
                                    placeholder="请输入地址" />
                            </el-form-item>
                            <el-form-item label="更新内容" prop="msg">
                                <el-input v-model="form.msg" type="textarea" placeholder="请输入内容" :rows="3" />
                            </el-form-item>
                        </el-form>
                        <div class="demo-drawer__footer">
                            <el-button type="primary" @click="submitForm">确 定</el-button>
                            <el-button @click="cancel">取 消</el-button>
                        </div>
                    </div>
                </el-drawer>
            </el-card>
        </template>
    </BasicLayout>
</template>
  
<script>
import { getVersionList, addVersion, editVersion, delVersion } from '@/api/admin/sys-config'
export default {
    name: 'Version',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            chooseIds: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 表格数据
            dataList: [],
            // 排序字段
            order: 'createdAtOrder',
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageIndex: 1,
                pageSize: 10,
                province: undefined
            },
            form: {},
            rules: {
                name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
                province: [{ required: true, message: '选择适用地区', trigger: 'blur' }]
            },
        }
    },

    created() {
        this.getList()
    },
    methods: {
        /** 查询参数列表 */
        getList() {
            this.loading = true
            getVersionList(this.addDateRange(this.queryParams, this.dateRange)).then(
                response => {
                    this.dataList = response.data.list
                    this.total = response.data.count
                    this.loading = false
                }
            )
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageIndex = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm('queryForm')
            this.handleQuery()
        },
        handleSortChang(column, prop, order) {
            prop = column.prop
            order = column.order
            if (this.order !== '' && this.order !== prop + 'Order') {
                this.queryParams[this.order] = undefined
            }
            if (order === 'descending') {
                this.queryParams[prop + 'Order'] = 'desc'
                this.order = prop + 'Order'
            } else if (order === 'ascending') {
                this.queryParams[prop + 'Order'] = 'asc'
                this.order = prop + 'Order'
            } else {
                this.queryParams[prop + 'Order'] = undefined
            }
            this.getList()
        },

        /** 删除按钮操作 */
        handleDelete(row) {
            this.$confirm('是否确认删除', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function () {
                    return delVersion(row.id)
                })
                .then(response => {
                    if (response.code === 200) {
                        this.msgSuccess(response.msg)
                        this.getList()
                    } else {
                        this.msgError(response.msg)
                    }
                })
                .catch(function () { })
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '新增版本'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            this.form = JSON.parse(JSON.stringify(row))
            this.open = true
            this.title = '修改版本'
        },
        // 表单重置
        reset() {
            this.form = {}
            this.resetForm('form')
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id) {
                        editVersion({
                            ...this.form,
                        }).then(res => {
                            if (res.code === 200) {
                                this.msgSuccess(res.msg)
                                this.open = false
                                this.getList()
                            } else {
                                this.msgError(res.msg)
                            }
                        })
                        return
                    }
                    addVersion({
                        ...this.form,
                    }).then(res => {
                        if (res.code === 200) {
                            this.msgSuccess(res.msg)
                            this.open = false
                            this.getList()
                        } else {
                            this.msgError(res.msg)
                        }
                    })
                }
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },

        selectSystem(val) {
            this.reset()
        }
    }
}
</script>
  
<style>
.el-tag {
    margin-right: 5px;
}

.button-new-tag {
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
}

.input-new-tag {
    width: 90px;
    vertical-align: bottom;
}
</style>
  